package com.gywj.race.controller.api;

import com.gywj.race.config.page.PageParam;
import com.gywj.race.config.page.PageResult;
import com.gywj.race.config.result.AjaxResultHandler;
import com.gywj.race.dto.EventGroupAddDto;
import com.gywj.race.dto.EventInformationAddDto;
import com.gywj.race.dto.EventPriceAddDto;
import com.gywj.race.entity.EventGroup;
import com.gywj.race.entity.EventInformation;
import com.gywj.race.entity.EventPrice;
import com.gywj.race.service.EventService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * The type Event controller.
 */
@RestController
@RequestMapping("/event")
public class EventController {
    @Resource
    private EventService eventService;

    /**
     * 添加赛事组别
     *
     * @param eventGroupAddDto the event group add dto
     * @return the map
     */
    @PostMapping("/group/add")
    public Map<String, Object> addGroup(@RequestBody EventGroupAddDto eventGroupAddDto, HttpServletRequest request) {
        try {
            eventService.addGroup(eventGroupAddDto, request);
            return AjaxResultHandler.Success("插入成功");
        } catch (Exception e) {

            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * 获取组别列表 下拉框
     *
     * @param eventId the event id
     * @return the group list
     */
    @PostMapping("/group/list")
    public Map<String, Object> getGroupList(@RequestParam Integer eventId) {
        try {
            List<EventGroup> eventGroups = eventService.getGroupList(eventId);
            return AjaxResultHandler.Success("成功", eventGroups);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * 获取组别列表 分页
     *
     * @param pageParam the page param
     * @return the group list page
     */
    @PostMapping("/group/list/page")
    public Map<String, Object> getGroupListPage(@RequestBody PageParam pageParam) {
        try {
            PageResult groupListPage = eventService.getGroupListPage(pageParam);
            return AjaxResultHandler.Success("成功", groupListPage);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * 添加赛事套餐
     *
     * @param eventPriceAddDto the event price add dto
     * @return the map
     */
    @PostMapping("/price/add")
    public Map<String, Object> addPrice(@RequestBody EventPriceAddDto eventPriceAddDto) {
        try {
            eventService.addPrice(eventPriceAddDto);
            return AjaxResultHandler.Success("插入成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * 获取赛事套餐列表 - 下拉框
     *
     * @param eventId the event id
     * @return the price list
     */
    @PostMapping("/price/list")
    public Map<String, Object> getPriceList(@RequestParam Integer eventId) {
        try {
            List<EventPrice> eventPrice = eventService.getPriceList(eventId);
            return AjaxResultHandler.Success("成功", eventPrice);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * 获取赛事套餐列表 - 分页
     *
     * @param pageParam the page param
     * @return the price list page
     */
    @PostMapping("/price/list/page")
    public Map<String, Object> getPriceListPage(@RequestBody PageParam pageParam) {
        try {
            PageResult priceListPage = eventService.getPriceListPage(pageParam);
            return AjaxResultHandler.Success("成功", priceListPage);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }


    /**
     * 添加赛事信息
     *
     * @param eventInformationAddDto the event information add dto
     * @param request                the request
     * @return the map
     */
    @PostMapping("/information/add")
    public Map<String, Object> addInformation(@RequestBody EventInformationAddDto eventInformationAddDto, HttpServletRequest request) {
        try {
            eventService.addInformation(eventInformationAddDto, request);
            return AjaxResultHandler.Success("插入成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * 更新赛事信息
     *
     * @param eventInformation the event information add dto
     * @return the map
     */
    @PostMapping("/information/update")
    public Map<String, Object> updateInformation(@RequestBody EventInformation eventInformation) {
        try {
            eventService.updateInformation(eventInformation);
            return AjaxResultHandler.Success("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * 删除赛事信息
     *
     * @param eventId the event id
     * @return the map
     */
    @PostMapping("/information/delete/byId")
    public Map<String, Object> deleteEventById(@RequestParam Integer eventId) {
        try {
            eventService.deleteEventById(eventId);
            return AjaxResultHandler.Success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }


    /**
     * 根据id删除套餐
     *
     * @param priceId the price id
     * @return the map
     */
    @PostMapping("/price/delete/byId")
    public Map<String, Object> deleteEventPriceById(@RequestParam Integer priceId) {
        try {
            eventService.deleteEventPriceById(priceId);
            return AjaxResultHandler.Success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }


    /**
     * 根据id 删除组别
     *
     * @param groupId the group id
     * @return the map
     */
    @PostMapping("/group/delete/byId")
    public Map<String, Object> deleteEventGroupById(@RequestParam Integer groupId) {
        try {
            eventService.deleteEventGroupById(groupId);
            return AjaxResultHandler.Success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }


    /**
     * 获取赛事信息列表 - 下拉框
     *
     * @return the information list
     */
    @PostMapping("/information/list")
    public Map<String, Object> getInformationList() {
        try {
            List<EventInformation> eventInformation = eventService.getInformationList();
            return AjaxResultHandler.Success("成功", eventInformation);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * 根据id获取赛事信息
     *
     * @return the information list
     */
    @PostMapping("/information/isAble")
    public Map<String, Object> getEventInformationIsAble(@RequestParam("id") Integer id) {
        try {
            eventService.getEventInformationIsAble(id);
            return AjaxResultHandler.Success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

    /**
     * 获取赛事信息列表 - 分页
     *
     * @param pageParam the page param
     * @return the information list page
     */
    @PostMapping("/information/list/page")
    public Map<String, Object> getInformationListPage(@RequestBody PageParam pageParam) {
        try {
            PageResult informationListPage = eventService.getInformationListPage(pageParam);
            return AjaxResultHandler.Success("成功", informationListPage);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }
}
